import sys

from PyQt5 import QtGui, QtCore, QtWidgets

from cude_generator import CubeData
from ui.main_window import Ui_MainWindow
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QColorDialog, QFileDialog


class CubeWallMain(QMainWindow):
    title_default = "魔方墙v1.0 - https://gitee.com/tangyang/cube-wall"

    def __init__(self):
        super(CubeWallMain, self).__init__()

        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.setWindowTitle(CubeWallMain.title_default)

        self.cube_data = None
        self.image_path = None
        # self.setWindowIcon(QtGui.QIcon(QtGui.QPixmap("./brain.ico")))
        self.ui.btn_gen.clicked.connect(self.on_gen_click)
        self.ui.btn_full.clicked.connect(self.on_full_click)
        self.ui.btn_bg.clicked.connect(self.on_bg_click)

        # self.ui.btn_image.clicked.connect(self.on_image_click)
        self.ui.btn_image.setVisible(False)
        self.ui.combo_opacity.setVisible(False)

        self.ui.check_rst.stateChanged.connect(self.on_check_change)

    def on_image_click(self):
        image_path, imgType = QFileDialog.getOpenFileName(self, "打开图片", ".", "*.jpg;*.png;*.jpeg")
        print(image_path, imgType)
        self.image_path = image_path

    def on_bg_click(self):
        col = QColorDialog.getColor()
        if col.isValid():
            style_sheet = f"background-color: {col.name()}"
            print(style_sheet)
            self.ui.centralwidget.setStyleSheet(style_sheet)

    def on_gen_click(self):
        self.gen_new_cube_data()

    def gen_new_cube_data(self):
        self.ui.check_rst.setChecked(QtCore.Qt.Unchecked)

        wall_width = self.ui.spin_width.value()
        wall_height = self.ui.spin_height.value()
        cube_size = self.ui.spin_size.value()
        diff_count = self.ui.spin_count.value()
        border_size = self.ui.spin_border.value()
        current_opacity = self.ui.combo_opacity.currentIndex()

        if diff_count > (wall_width * wall_height * 0.5):
            QMessageBox.warning(self, "未创建成功！",
                                "不同颜色的数量【{}】超过总数量【{}】的一半".format(diff_count, int(wall_width * wall_height)))
            return

        print(wall_width, wall_height, cube_size, diff_count)
        self.cube_data = CubeData.gen_data(wall_width, wall_height,
                                           cube_size, diff_count,
                                           border_size, self.image_path, current_opacity)

        self.update_window_title()

        self.ui.eye_left.set_mouse_listener(self.on_mouse_event)
        self.ui.eye_right.set_mouse_listener(self.on_mouse_event)

        self.ui.eye_left.init(self.cube_data, 0)
        self.ui.eye_right.init(self.cube_data, 1)

    def update_window_title(self):
        subfix = ""
        if self.cube_data is not None:
            subfix = " 已尝试{}次，还有【{}】个不同".format(self.cube_data.global_try_diff_count,
                                                self.cube_data.global_diff_count)
        self.setWindowTitle(CubeWallMain.title_default + subfix)

    def on_mouse_event(self, event, c_pos, pos):
        self.ui.eye_left.trigger_mouse_event(event, c_pos, pos)
        self.ui.eye_right.trigger_mouse_event(event, c_pos, pos)

        if event == 0:
            if c_pos is not None:
                self.statusBar().showMessage(f"x: {c_pos.x()}  y: {c_pos.y()}")
            else:
                self.statusBar().clearMessage()

        elif event == 1:
            if self.cube_data is None:
                return

            self.update_window_title()

            if self.cube_data.global_diff_count == 0:
                reply = QMessageBox.information(self, "恭喜！", "祝贺你，你找到了所有的不同之处，不愧是天选之子！\n是否开始新的一局？",
                                                QMessageBox.Yes | QMessageBox.No,
                                                defaultButton=QMessageBox.No)
                if reply == QMessageBox.Yes:
                    self.gen_new_cube_data()

    def on_full_click(self):
        if self.isFullScreen():
            self.showNormal()
        else:
            self.showFullScreen()

    def on_check_change(self, state):
        self.ui.eye_left.show_diff_result(state == QtCore.Qt.Checked)
        self.ui.eye_right.show_diff_result(state == QtCore.Qt.Checked)

    # def keyPressEvent(self, event: QtGui.QKeyEvent) -> None:
    #     if event.key() == QtCore.Qt.Key_Enter:


if __name__ == '__main__':
    app = QApplication(sys.argv)

    window = CubeWallMain()
    window.show()

    sys.exit(app.exec_())
